package com.action.ui.activity.user;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import com.action.childbase.ChildBaseActivity;
import com.action.ui.adapter.ComFragmentAdapter;
import com.action.ui.fragment.XYBREDHistoryRecvFrag;
import com.action.ui.fragment.SendHistoryRedPacketFrag;
import com.action.ui.view.ColorFlipPagerTitleView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.util.AppConstants;
import com.xinglai.yun.R;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = AppConstants.ROUTEURI.RED_PACKET_HISTROY)
public class RedPacketHistory extends ChildBaseActivity<XYBUPressenter> {

    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;

    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private XYBREDHistoryRecvFrag XYBREDHistoryRecvFrag;

    private SendHistoryRedPacketFrag sendHistoryRedPacketFrag;

    private CommonNavigatorAdapter commonNavigatorAdapter;

    private List<String> mDataList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_red_packet_history;
    }

    @Override
    protected void initView() {
        super.initView();
        setToolBar("全城红包");
        XYBREDHistoryRecvFrag = new XYBREDHistoryRecvFrag();
        sendHistoryRedPacketFrag = new SendHistoryRedPacketFrag();
        fragmentList.add(XYBREDHistoryRecvFrag);
        fragmentList.add(sendHistoryRedPacketFrag);
        mDataList.add("收到的红包");
        mDataList.add("我发的发红");
        viewPager.setAdapter(new ComFragmentAdapter(getSupportFragmentManager(), fragmentList));
        viewPager.setOffscreenPageLimit(2);

        initMagicIndicator();
    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(true);
        commonNavigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(RedPacketHistory.this, R.color.black));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(RedPacketHistory.this, R.color.red));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        nowIndex = index;
                        viewPager.setCurrentItem(index, false);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, AppConstants.LINE_CURSOR_WIDTH));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(RedPacketHistory.this, R.color.red));
                return indicator;
            }
        };
        commonNavigator.setAdapter(commonNavigatorAdapter);
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    @Override
    protected void initData() {
        super.initData();
        if (null != mPresenter) {
            mPresenter.fetchRecvSndRedPacketCount();
        }
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    protected List<Fragment> fragmentList = new ArrayList<>();
    protected int nowIndex = 0;

}
